package com.wf.course.web.controller.course;

import com.utils.page.PageBean;
import com.wf.course.model.course.Courselogs;
import com.wf.course.service.ICourseLogsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.lang.reflect.Field;

@Controller
@RequestMapping(value = "course")
public class CourseLogsController {

    Logger logger = LoggerFactory.getLogger(CourseLogsController.class);

    @Autowired
    private ICourseLogsService courseLogsService;

    @RequestMapping("logs")
    public String logs(Courselogs courselogs, Model model,
                       @RequestParam(value = "pageNo", required = false) Integer pageNo,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PageBean<Courselogs> pageBean = null;
        try {
            pageBean = new PageBean<>(pageNo, pageSize);
            pageBean.setObj(courselogs);
            courseLogsService.logs(pageBean);
            pageBean.setUrl("course/logs");
            StringBuilder builder = new StringBuilder();
            for (Field f: courselogs.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                if (null != f.get(courselogs) && StringUtils.isNotBlank(f.get(courselogs).toString())) {
                    builder.append("&" + f.getName() + "=" + f.get(courselogs));
                }
            }
            pageBean.setParams(builder.toString());
            model.addAttribute("page", pageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "course/courseLogs";

    }
}
